import { Component, OnDestroy } from '@angular/core';

import { BrandShopService } from './brand.shop.service';

import { Util } from '../../../utils/util';


import { ShopBase } from '../../shop/shop.base';

import { ToastyService } from '../../../modules/ng2-toasty';
import { AsyncLocalStorage } from 'angular-async-local-storage';
@Component({
  selector: 'brand-shop',
  templateUrl: './brand.shop.html',
  providers: [BrandShopService]
})

export class BrandShopComponent extends ShopBase {


  // 参数
  param: any = {
    start: 0,
    rows: 10,
    sort: 'sellMoney',
    sortType: 'desc'
  };
  // 列
  public columns: Array<any> = [
  {
    title: '店铺名称',
    name: 'brandName',
    sort: false
  },
  {
    title: '品牌日销售额',
    name: 'sellMoney',
    sort: true
  },
  {
    title: '品牌日销售量',
    name: 'sellAmount',
    sort: true
  },
  {
    title: '品牌近30天销售额',
    name: 'monthSellMoney',
    sort: true
  },
  {
    title: '品牌近30天销售量',
    name: 'monthSellAmount',
    sort: true
  }
  ];

  brandInfo: any;

  catNameList: any = [];
  constructor(public util: Util, public service: BrandShopService, public toastyService: ToastyService, public asyncStore: AsyncLocalStorage) {

    super(util, service);

    this.brandInfo = this.util.sessionStore().get('brandDetail');

    this.param.brandName = this.brandInfo.brandName;
    this.param.categoryId = this.brandInfo.categoryId || this.brandInfo.category;

    this.asyncStore.getItem('brandCatgory').subscribe((dataList) => {
      console.log(dataList);
      this.catNameList = dataList;
    });

    this.getHttp(this.param);
  }

  ngOnDestroy() {
    if (this.busy) {
      this.busy.unsubscribe();
    }
    if (this.busy2) {
      this.busy2.unsubscribe();
    }
  }

  getHttp(param) {
    var res = {"code":0,"msg":"成功","data":{"value":[{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"newbalance童鞋旗舰店","uid":"1119887712","shopType":"商城","sellAmount":2830,"sellMoney":594408.0,"monthSellAmount":161868,"monthSellMoney":3.5743204E7},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"New Balance旗舰店","uid":"520557274","shopType":"商城","sellAmount":1129,"sellMoney":373300.0,"monthSellAmount":815260,"monthSellMoney":2.538585633E8},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"小菜代购","uid":"722965367","shopType":"C店","sellAmount":94,"sellMoney":64394.0,"monthSellAmount":1832,"monthSellMoney":1346912.0},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"源本体育","uid":"651497337","shopType":"C店","sellAmount":45,"sellMoney":41716.0,"monthSellAmount":790,"monthSellMoney":651320.0},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"小靓仔正品","uid":"1659771931","shopType":"C店","sellAmount":42,"sellMoney":36078.0,"monthSellAmount":583,"monthSellMoney":488658.7},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"newbalance嘉润专卖店","uid":"1032439459","shopType":"商城","sellAmount":74,"sellMoney":30987.0,"monthSellAmount":9659,"monthSellMoney":4561302.0},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"心随衣动5588","uid":"676017593","shopType":"C店","sellAmount":24,"sellMoney":23130.0,"monthSellAmount":245,"monthSellMoney":234354.0},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"南京小鞋匠","uid":"83531154","shopType":"C店","sellAmount":24,"sellMoney":21339.0,"monthSellAmount":653,"monthSellMoney":529896.0},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"鞋之正义","uid":"124055820","shopType":"C店","sellAmount":22,"sellMoney":18076.0,"monthSellAmount":1440,"monthSellMoney":1315600.0},{"date":"2017-12-03 00:00:00","categoryId":"-1","categoryName":"/","categoryLevel":0,"brandName":"new balance","shopName":"宝骏运动专营店","uid":"2761413550","shopType":"商城","sellAmount":42,"sellMoney":17078.0,"monthSellAmount":1392,"monthSellMoney":531818.0}],"count":782}};
    setTimeout(() => {

      this.vm.table.shops = res.data.value;

      this.vm.table.count = res.data.count;

    }, 100);
  }

  /*//类目
  catNameChange(index = -1) {

    if(index == -1){

      this.param.categoryId = -1;
      this.param.catName = '';
      this.getHttp(this.param);
      return;
    }

    let item = this.catNameList[index];

    if (item.categoryId == this.param.categoryId) {
      return;
    }
    this.param.categoryId = item.categoryId;
    this.param.catName = item.categoryName;
    this.getHttp(this.param);
  }*/
  //
  searchText: any;
  search() {
    if(this.param.uid == ''){
      this.searchText = this.param.uid;
      delete this.param.uid;
      this.getHttp(this.param);
      return;
    }

    if (this.searchText == this.param.uid) {
      return;
    }
    this.searchText = this.param.uid;
    this.getHttp(this.param);
  }


  busy2: any;
  export() {

    let json = Object.assign({}, this.param);

    json.rows = 1000;
    json.start = 0;

    // toast
    let toastOptions = {
      title: "",
      msg: "下载中...",
      showClose: true,
      timeout: 6000,
      theme: 'material'
    };
    // Add see all possible types in one shot
    this.toastyService.info(toastOptions);

    this.busy2 = this.service.getBrandShop(json).subscribe((res: any) => {


      let dim = ['date', 'categoryId', 'categoryName', 'categoryLevel', 'brandName', 'shopName', 'uid', 'shopType', 'monthSellAmount', 'monthSellMoney', 'sellAmount', 'sellMoney'];

      let dimText = ['时间', '类目id', '类目', '类目等级', '品牌名', '店铺名', '店铺id', '店铺类型', '月销售量', '月销售额', '销售量', '销售额'];



      let fileName = this.brandInfo.brandName + this.brandInfo.categoryName + "全部店铺";


      let data = this.util.jsonToArrry(res.data.value, dim, dimText);

      this.util.export(data, fileName);


    }, (err: any) => {

    });
  }
}
